package top.eggcode.plugins.query.predicate;

import org.apache.commons.lang3.StringUtils;
import top.eggcode.plugins.query.model.Expression;
import top.eggcode.plugins.query.model.ExpressionImpl;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class InPredicate<T> extends AbstractPredicateImpl {

    private final Expression<String> exp;

    private final List<T> values;

    public InPredicate(Expression<String> exp, Collection<T> values) {
        this.exp = exp;
        this.values = new LinkedList<>();
        this.values.addAll(values);
    }

    public InPredicate(Expression<String> exp, List<T> values) {
        this.exp = exp;
        this.values = values;
    }

    public InPredicate(Expression<String> exp, T... values) {
        this.exp = exp;
        this.values = Arrays.asList(values);
    }


    @Override
    public String render() {
        return render(isNegated());
    }

    @Override
    public String render(boolean isNegated) {
        if (values.size() == 0) {
            return null;
        }

        StringBuilder builder = new StringBuilder();

        List<String> valuesOfString = new LinkedList<>();
        values.forEach(item -> {
            valuesOfString.add(String.valueOf(item));
        });
        builder.append(exp.getValue());
        if (isNegated) {
            builder.append(" NOT");
        }
        builder.append(" IN ")
                .append(" ( ")
                .append(StringUtils.join(valuesOfString, ", "))
                .append(" )");
        return builder.toString();
    }

    @Override
    public List<Expression<Boolean>> getExpressions() {
        return null;
    }

    public static void main(String[] args) {
        InPredicate<Integer> in = new InPredicate<Integer>(new ExpressionImpl<>("a.class_id", String.class), 1, 2, 3, 4, 5);

        System.out.println(in.render(true));
    }


}
